package IC.Semantics.symbols;

import java.util.LinkedHashMap;

public abstract class SymbolTable
{
	protected final SymbolTable parent;
	protected LinkedHashMap<String, SymbolTable> children;
	protected LinkedHashMap<String, SemanticSymbol> entries;
	protected String name;
	
	public SymbolTable(SymbolTable parent, String name)
	{
		this.parent = parent;
		this.name = name;
		this.entries = new LinkedHashMap<String, SemanticSymbol>();
		this.children = new LinkedHashMap<String, SymbolTable>();
	}
	
	public String getName(){
		return name;
	}
	
	public SymbolTable getParent(){
		return parent;
	}

	public void insert(SemanticSymbol s){
		entries.put(s.name, s);
	}

	public SemanticSymbol lookup(String name)
	{
		if (entries.containsKey(name))
			return entries.get(name);
		else if (parent != null)
			return parent.lookup(name);
		else
			return null; 
	}
	
	/**
	 * look only in the current scope only for the given symbol name
	 * @param name
	 * @return
	 */
	public SemanticSymbol localLookup(String name){
		if (entries.containsKey(name))
			return entries.get(name);
		else return null;
	}
	
	public SymbolTable getTable(String name){
		return children.get(name);
	}
	
	public void addTable(String name, SymbolTable st){
		children.put(name, st);
	}
	
	public abstract String dump();
}


